package org.xxpay.mch.common.ctrl;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.LocaleResolver;
import org.xxpay.core.common.domain.I18nBizResponse;
import org.xxpay.core.common.util.I18nUtil;
import org.xxpay.mch.common.ctrl.BaseController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * 国际化控制器 - 商户系统
 * 提供语言切换和国际化信息获取接口
 * 
 * @author xxpay
 */
@RestController
@RequestMapping("/api/i18n")
public class I18nController extends BaseController {

    @Resource
    private LocaleResolver localeResolver;

    /**
     * 切换语言
     * 
     * @param lang 语言代码 (zh_CN, en_US)
     * @param request HTTP请求
     * @param response HTTP响应
     * @return 响应结果
     */
    @PostMapping("/switch")
    public I18nBizResponse switchLanguage(@RequestParam String lang,
                                         HttpServletRequest request,
                                         HttpServletResponse response) {
        try {
            Locale locale;
            switch (lang) {
                case "zh_CN":
                    locale = Locale.SIMPLIFIED_CHINESE;
                    break;
                case "en_US":
                    locale = Locale.US;
                    break;
                default:
                    return I18nBizResponse.paramError("不支持的语言: " + lang);
            }
            
            // 设置语言
            localeResolver.setLocale(request, response, locale);
            
            // 更新工具类的当前语言
            I18nUtil.setCurrentLocale(lang);
            
            return I18nBizResponse.success("language.switch.success");
        } catch (Exception e) {
            return I18nBizResponse.error("language.switch.failed");
        }
    }

    /**
     * 获取当前语言信息
     * 
     * @return 当前语言信息
     */
    @GetMapping("/current")
    public I18nBizResponse getCurrentLanguage() {
        Map<String, Object> data = new HashMap<>();
        data.put("locale", I18nUtil.getCurrentLocale());
        data.put("language", I18nUtil.getCurrentLocale().split("_")[0]);
        data.put("country", I18nUtil.getCurrentLocale().split("_").length > 1 ? 
                 I18nUtil.getCurrentLocale().split("_")[1] : "");
        data.put("isChinese", I18nUtil.isChinese());
        data.put("isEnglish", I18nUtil.isEnglish());
        
        return I18nBizResponse.success(data);
    }

    /**
     * 获取支持的语言列表
     * 
     * @return 支持的语言列表
     */
    @GetMapping("/languages")
    public I18nBizResponse getSupportedLanguages() {
        Map<String, Object> languages = new HashMap<>();
        
        Map<String, String> zhCN = new HashMap<>();
        zhCN.put("code", "zh_CN");
        zhCN.put("name", "中文");
        zhCN.put("englishName", "Chinese");
        
        Map<String, String> enUS = new HashMap<>();
        enUS.put("code", "en_US");
        enUS.put("name", "English");
        enUS.put("englishName", "English");
        
        languages.put("zh_CN", zhCN);
        languages.put("en_US", enUS);
        
        return I18nBizResponse.success(languages);
    }

    /**
     * 获取国际化消息
     * 
     * @param key 消息键
     * @param args 参数（可选）
     * @return 国际化消息
     */
    @GetMapping("/message")
    public I18nBizResponse getMessage(@RequestParam String key,
                                     @RequestParam(required = false) String[] args) {
        try {
            String message = I18nUtil.getMessage(key, (Object[]) args);
            Map<String, Object> data = new HashMap<>();
            data.put("key", key);
            data.put("message", message);
            data.put("locale", I18nUtil.getCurrentLocale());
            
            return I18nBizResponse.success(data);
        } catch (Exception e) {
            return I18nBizResponse.error("message.not.found");
        }
    }

    /**
     * 批量获取国际化消息
     * 
     * @param keys 消息键列表
     * @return 国际化消息映射
     */
    @PostMapping("/messages")
    public I18nBizResponse getMessages(@RequestBody String[] keys) {
        try {
            Map<String, String> messages = new HashMap<>();
            for (String key : keys) {
                messages.put(key, I18nUtil.getMessage(key));
            }
            
            Map<String, Object> data = new HashMap<>();
            data.put("messages", messages);
            data.put("locale", I18nUtil.getCurrentLocale());
            
            return I18nBizResponse.success(data);
        } catch (Exception e) {
            return I18nBizResponse.error("messages.load.failed");
        }
    }
}
